package cn.tedu._03vehicle.base.exception;

import cn.tedu._03vehicle.base.response.JsonResult;
import cn.tedu._03vehicle.base.response.StatueCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import javax.validation.ConstraintViolationException;

@Slf4j
@RestControllerAdvice
public class GlobalExceptionHandler {
    @ExceptionHandler
    public JsonResult doHandle(IllegalArgumentException ex){
        String data=ex.getMessage();
        log.error("IllegalArgumentException:{}",data);
        return new JsonResult(StatueCode.OPERATION_FAILED,data);
    }

    @ExceptionHandler
    public JsonResult doHandleRuntimeException(RuntimeException ex){
        String data=ex.getMessage();
        log.error("RuntimeException:{}",data);
        return new JsonResult(StatueCode.OPERATION_FAILED,data);
    }
//    @ExceptionHandler
//    public JsonResult doHandleThrowable(Throwable ex){
//        String data=ex.getMessage();
//        log.error("Throwable:{}"+data);
//        return new JsonResult(StatueCode.THROWABLE_ERROR,data);
//    }
    @ExceptionHandler
    public JsonResult doHandleMethodArgumentNotValidException(MethodArgumentNotValidException ex){
        String data=ex.getMessage();
        log.error("MethodArgumentNotValidException:{}",data);
        return new JsonResult(StatueCode.OPERATION_FAILED,data);
    }

    @ExceptionHandler
    public JsonResult doHandle(ConstraintViolationException ex){
        String data=ex.getMessage();
        log.error("ConstraintViolationException:{}",data);
        return new JsonResult(StatueCode.OPERATION_FAILED,data);
    }

}
